package com.liao.furns.web; /**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */

import com.liao.furns.pojo.Cart;
import com.liao.furns.pojo.Member;
import com.liao.furns.pojo.Order;
import com.liao.furns.pojo.OrderItem;
import com.liao.furns.service.OrderService;
import com.liao.furns.service.impl.OrderServiceImpl;
import com.liao.furns.utils.JDBCUtilsByDruid;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebServlet(name = "OrderServlet", value = "/OrderServlet")
public class OrderServlet extends BasicServlet {
    private OrderService orderService = new OrderServiceImpl();

    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart =(Cart) request.getSession().getAttribute("cart");
//        System.out.println("jinglaile");
        Member member = (Member)request.getSession().getAttribute("member");
//        System.out.println("jinglaile");
        if(member == null){
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
            return;
        }
        if(cart == null || cart.isEmpty()){
            request.getRequestDispatcher("/index.jsp").forward(request,response);
            return;
        }
//        System.out.println(member);
        Integer id = member.getId();
//        System.out.println(id);
        String orderId = null;
        orderId = orderService.saveOrder(cart, member.getId());
        request.getSession().setAttribute("orderId",orderId);
        response.sendRedirect(request.getContextPath() + "/views/order/checkout.jsp");
    }

    protected void queryOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Member member = (Member)request.getSession().getAttribute("member");
        int  id =member.getId();
        List<Order> orders = orderService.queryOrderByMemberID(id);
        request.getSession().setAttribute("orders",orders);
        response.sendRedirect(request.getContextPath() + "/views/order/order.jsp");
    }

    protected void queryOrderItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String orderId = (String)request.getSession().getAttribute("orderId");
        String orderId = (String)request.getParameter("orderId");
        List<OrderItem> orderItems = orderService.queryOrderItemByOrderID(orderId);
        System.out.println(orderItems);
        request.getSession().setAttribute("orderItems",orderItems);
        BigDecimal totalPrice = new BigDecimal(0);
        int count = 0;
        for (OrderItem orderItem : orderItems) {
            totalPrice = totalPrice.add(orderItem.getTotalPrice());
            System.out.println(orderItem.getTotalPrice());
        }
        for (OrderItem orderItem : orderItems) {
            count += orderItem.getCount();
        }
        System.out.println(totalPrice);
        request.getSession().setAttribute("totalPrice",totalPrice);
        request.getSession().setAttribute("count",count);
        response.sendRedirect(request.getContextPath() + "/views/order/order_detail.jsp");
    }
}
